package com.qzsoft.zhjd.laboratory.modules.groutingRecord.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.laboratory.modules.groutingRecord.entity.GroutingRecord;
import com.qzsoft.zhjd.laboratory.modules.groutingRecord.service.GroutingRecordService;
import com.qzsoft.zhjd.laboratory.modules.tensionResult.entity.TensionResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 压浆记录(GroutingRecord)表控制层
 *
 * @author makejava
 * @since 2023-07-12 14:58:59
 */
@RestController
@AllArgsConstructor
@RequestMapping("/laboratory/groutingRecord")
public class GroutingRecordController {

    private GroutingRecordService groutingRecordService;

    /**
     * 分页
     *
     * @param params 查询条件
     * @return Result 分页列表
     */
    @GetMapping("page")
    public Result<IPage<GroutingRecord>> page(@RequestParam Map<String, Object> params) {
        IPage<GroutingRecord> page = groutingRecordService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/find/{id}")
    public Result selectOne(@PathVariable("id") String id) {
        return ResultUtils.ok(groutingRecordService.getInfoById(id));
    }

    /**
     * 新增数据
     *
     * @param groutingRecord 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody GroutingRecord groutingRecord) {
        if(groutingRecord.getCode()!=null){
            QueryWrapper<GroutingRecord> queryWrapper = new QueryWrapper<GroutingRecord>();
            queryWrapper.eq("code",groutingRecord.getCode());
            queryWrapper.eq("step_parameters",groutingRecord.getStepParameters());
            List<GroutingRecord>  result = groutingRecordService.list(queryWrapper);
            if(result.size()==0){
                groutingRecordService.insertGroutingRecord(groutingRecord);
            }
        }else{
            groutingRecordService.insertGroutingRecord(groutingRecord);
        }
        return ResultUtils.ok();
    }

    /**
     * 压浆数据接收接口
     * @param groutingJson
     * @return
     */
    @PostMapping("/groutingDataReception")
    public Result groutingDataReception(@RequestBody JSONObject groutingJson) {
        return groutingRecordService.groutingDataReception(groutingJson);
    }

    /**
     * 修改数据
     *
     * @param groutingRecord 实体对象
     * @return 修改结果
     */
    @PutMapping("updateGroutingRecord")
    public Result update(@RequestBody GroutingRecord groutingRecord) {
        groutingRecordService.updateGroutingRecord(groutingRecord);
        return ResultUtils.ok();
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping("deleteByIds")
    public Result delete(@RequestBody String[] ids) {
        groutingRecordService.deleteByIds(Arrays.asList(ids));
        return ResultUtils.ok();
    }
}

